package com.panopset.game.blackjack;

import com.panopset.game.card.Card;

/**
 * Blackjack card.
 *
 * @author Karl Dinwiddie
 *
 */
public class BlackjackCard {

    /**
     * Playing card.
     */
    private Card c;

    public Card getCard() {
        return c;
    }

    /**
     *
     * @param card
     *            Playing card.
     */
    public BlackjackCard(final Card card) {
        this(card, true);
    }

    public BlackjackCard(Card card, boolean isShowing) {
        c = card;
        if (isShowing) {
            c.show();
            count();
        }
    }

    /**
     *
     * @return true iff face offset is 0.
     */
    public boolean isAce() {
        return c.getFace().getOffset() == 0;
    }

    /**
     *
     * @return Blackjack soft value of this card.
     */
    public int getSoftValue() {
        if (isAce()) {
            return 11;
        }
        return getNonAceValue();
    }

    /**
     *
     * @return Blackjack hard value of this card.
     */
    public int getHardValue() {
        if (isAce()) {
            return 1;
        }
        return getNonAceValue();
    }

    /**
     *
     * @return Non ace value of this card.
     */
    private int getNonAceValue() {
        if (c.getFace().getOffset() < 10) {
            return c.getFace().getOffset() + 1;
        } else {
            return 10;
        }
    }

    private void count() {
        if (BlackjackGame.countingSystem != null) {
            BlackjackGame.countingSystem.count(this);
        }
    }

    public void show() {
        if (!c.isShowing()) {
            count();
        }
        c.show();
    }

    @Override
    public String toString() {
        return c.name();
    }
}
